package com.sdhs.paas.nativedbdemo.app;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import com.alibaba.fastjson.TypeReference;
import com.sdhs.paas.nativedbdemo.bean.UserBean;
import com.sdhs.paas.nativedbdemo.dto.UserDTO;
import com.sdhs.paas.nativedbdemo.dto.req.UserReqDTO;
import com.sdhs.paas.nativedbdemo.dto.res.UserResDTO;
import com.sdhs.paas.nativedbdemo.service.UserService;
import com.sdhs.paas.sdk.base.common.BasePage;
import com.sdhs.paas.sdk.base.dto.PageInfoDTO;
import com.sdhs.paas.sdk.base.page.Page;
import com.sdhs.paas.sdk.base.utils.EntityDtoConvertUtil;
import com.sdhs.paas.sdk.core.PaasBaseRequest;
import com.sdhs.paas.sdk.core.PaasBaseResponse;
import com.sdhs.paas.sdk.core.RefreshableRestController;
import com.sdhs.paas.sdk.core.logger.Logger;
import com.sdhs.paas.sdk.core.logger.LoggerFactory;

/**
 * Created by chenzhen on 2018/01/11.
 */
@RefreshableRestController
public class DemoController {
	
	private static Logger LOGGER  = LoggerFactory.getLogger(DemoController.class);  
	@Autowired
	private UserService userService;
	
	/**
	 * 分页查询
	 * */
	@PostMapping(value = "/getUserList")
	public PaasBaseResponse<UserResDTO> getUserList(@RequestBody PaasBaseRequest<UserReqDTO> userReqDTO) {
		UserBean confFile = EntityDtoConvertUtil.convertToEntity(userReqDTO.getBody().getUserDTO(),UserBean.class);
        PageInfoDTO pageInfoDTO = userReqDTO.getBody().getPageInfo();
		
		BasePage pageRequest = new BasePage();
		pageRequest.setPage(pageInfoDTO.getPage());
		pageRequest.setCount(pageInfoDTO.getPageSize());
		Page<UserBean> page = userService.getPageFileList(pageRequest, confFile);
		
		UserResDTO userResDTO = new UserResDTO();
		PageInfoDTO resPageInfoDTO = new PageInfoDTO();
		resPageInfoDTO.setPage(page.getCurPage());
        resPageInfoDTO.setPageSize(page.getPageSize());
        resPageInfoDTO.setPageCount(page.getPageCount());
        resPageInfoDTO.setRowCount(page.getTotalCount());
        userResDTO.setPageInfo(resPageInfoDTO);
        List<UserBean> confFileList = page.getResult();
        List<UserDTO> dtoList = new ArrayList<UserDTO>();
		dtoList = EntityDtoConvertUtil.convertToDTOList(confFileList, new TypeReference<List<UserDTO>>() {});
		userResDTO.setUserDTOList(dtoList);
		return new PaasBaseResponse<>(userResDTO);
	}
	/**
	 * 保存
	 * */
	@PostMapping(value = "/saveUser")
	public PaasBaseResponse<UserResDTO> saveUser(@RequestBody PaasBaseRequest<UserReqDTO> userReqDTO) {
		UserBean confFile = EntityDtoConvertUtil.convertToEntity(userReqDTO.getBody().getUserDTO(),UserBean.class);
		Boolean flag = userService.insert(confFile);
		UserResDTO userResDTO = new UserResDTO();
		return new PaasBaseResponse<UserResDTO>(userResDTO);
	}
	/**
	 * 获取详情
	 * */
	@PostMapping(value = "/getUserDetail")
	public PaasBaseResponse<UserResDTO> getUserDetail(@RequestBody PaasBaseRequest<UserReqDTO> userReqDTO) {
		UserBean userbean = EntityDtoConvertUtil.convertToEntity(userReqDTO.getBody().getUserDTO(),UserBean.class);
		userbean = userService.getUser(String.valueOf(userbean.getUid()));
		UserResDTO userResDTO = new UserResDTO();
		UserDTO userDTO = EntityDtoConvertUtil.convertToDTO(userbean, UserDTO.class);
		userResDTO.setUserDTO(userDTO);
		return new PaasBaseResponse<UserResDTO>(userResDTO);
	}
	
	@GetMapping("/testlog")
	public String testLog(){
//		logger.trace("I am trace log.");  
//        logger.debug("I am debug log.");  
//        logger.warn("I am warn log.");  
//        logger.error("I am error log.");  
//        PaasLogger.info("{}", "I am info log");
//        PaasLogger.debug("{}", "I am debug log");
//        PaasLogger.warn("{}", "I am warn log");
//        PaasLogger.error("{}", "I am error log");
		LOGGER.trace("Hello {}", "World");

		LOGGER.debug("Hello {}", "World");

		LOGGER.info("Hello {}", "World");

		LOGGER.warn("Hello {}", "World");

		LOGGER.error("Hello {}", "World");
		return "testlog";
	}
}
